package com.zmj.framework.handler.mybatis;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zmj.amc.sdk.entity.User;
import com.zmj.framework.util.StringUtil;
import com.zmj.framework.util.ThreadContextHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Import(SpringUtil.class)

/**
 * 数据持久化过滤器 添加默认值
 * @author Administrator
 */
@Component
public class MybatisFillHandler implements MetaObjectHandler {

    /**
     * 插入前初始化创建人创建时间等信息
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
//        IParameterService parameterService = SpringUtil.getBean(ParameterServiceImpl.class);
        User user = ThreadContextHandler.getCurrentUser();
        if (ObjectUtil.isNull(user)) {
            user = new User();
        }
        if (StrUtil.isBlank(user.getUsername())) {
            user.setUsername("default");
        }
        this.setFieldValByName("createUser", user.getUsername(), metaObject);
        // 满足LocalDateTime类型才填充,否则走原逻辑System.currentTimeMillis()填充
        String realName = user.getRealName();
        LocalDateTime localDateTime = LocalDateTime.now();
        this.strictInsertFill(metaObject, "createTime", () -> localDateTime, LocalDateTime.class);
        this.strictInsertFill(metaObject, "updateTime", () -> localDateTime, LocalDateTime.class);
        this.strictInsertFill(metaObject, "deleteFlag", () -> false, Boolean.class);
        this.strictInsertFill(metaObject, "deleteFlag", () -> 0L, Long.class);
        this.strictInsertFill(metaObject, "createUserName", () -> realName, String.class);

        if (StringUtil.isEmpty(metaObject.getValue("createTime"))) {
            this.setFieldValByName("createTime", System.currentTimeMillis(), metaObject);
        }

        if (ReflectUtil.hasField(metaObject.getOriginalObject().getClass(), "updateUser")) {
            this.setFieldValByName("updateUser", metaObject.getValue("createUser"), metaObject);
            if (StringUtil.isEmpty(metaObject.getValue("updateTime"))) {
                this.setFieldValByName("updateTime", System.currentTimeMillis(), metaObject);
            }
            this.setFieldValByName("isDeleted", "0", metaObject);
        }
    }

    /**
     * 修改前初始化修改人修改时间信息
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        User user = ThreadContextHandler.getCurrentUser();
        Assert.notNull(user, "未获取到当前用户信息，重新获取token后重试！");
//        Assert.notNull(user.getUsername(),"未获取到当前用户信息，重新获取token后重试！");

        this.setFieldValByName("updateUser", user.getUsername(), metaObject);
        // 满足LocalDateTime类型才填充,否则走原逻辑System.currentTimeMillis()填充
        this.strictUpdateFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class);
        if (StringUtil.isEmpty(metaObject.getValue("updateTime"))) {
            this.setFieldValByName("updateTime", System.currentTimeMillis(), metaObject);
        }
    }
}
